package pl.com.qapps.unity;

import pl.com.qapps.unity.UnityPb.ExceptionPb;
import pl.com.qapps.unity.UnityPb.Request;

/**
 * Common for all kinds of exception thrown Unity java API. Indicates internal
 * failure. Most likely impossible to recover.
 * 
 * @author Jakub Dykowski
 * 
 */
public class UnityException extends RuntimeException {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5531290879387996326L;
	private final boolean impl;
	private ExceptionPb cause;
	private Request request;

	UnityException(ExceptionPb cause) {
		this(cause, null);
	}

	UnityException(ExceptionPb cause, Request req) {
		this(cause.getMessage(), null, cause.getImpl());
		this.cause = cause;
		this.request = req;
	}

	/**
	 * 
	 */
	UnityException() {
		this((String) null, null);
	}

	/**
	 * @param message
	 */
	UnityException(String message) {
		this(message, null);
	}

	UnityException(String message, boolean impl) {
		this(message, null, impl);
	}

	/**
	 * @param cause
	 */
	UnityException(Throwable cause) {
		this(null, cause);
	}

	UnityException(Throwable cause, boolean impl) {
		this(null, cause, impl);
	}

	/**
	 * @param message
	 * @param cause
	 */
	UnityException(String message, Throwable cause) {
		this(message, cause, false);
	}

	UnityException(String message, Throwable cause, boolean impl) {
		super(message, cause);
		this.impl = impl;
		this.cause = null;
	}

	UnityException(UnityException other) {
		this(other.getMessage(), other.getCause(), other
				.isImplementationError());
		this.cause = other.cause;
	}

	final Request getRequest() {
		return request;
	}

	/**
	 * Indicates that internal API were misused.
	 * 
	 * @return
	 */
	final boolean isImplementationError() {
		return impl;
	}

	final ExceptionPb getError() {
		return cause;
	}
}
